/*------------------------------------------------------------------------------
 * Copyright 2012 NCR Corporation
 *------------------------------------------------------------------------------
 * All revision information is updated automatically from source code control
 * change records - please do not manually edit.
 *------------------------------------------------------------------------------
 *
 * $LastChangedRevision:: 2026                                     $
 * $LastChangedBy:: rb250096                                       $
 * $LastChangedDate:: 2012-04-07 18:34:30 -0400 (Sat, 07 Apr 2012) $
 */

package com.ncr.nep.dashboardcore.mvp.client;

import com.ncr.nep.dashboardcore.utils.client.ParameterizedDelegate;

/**
 * {@link Presenter} that managers a {@link DialogView} and contains logic
 * for notifying another object of when the dialog is closed and/or when
 * a resulting object is generated from the dialog.
 *
 * @param <T> The type of {@link DialogView} being managed by this presenter.
 * @param <R> The type of value returned as a result of the dialog and passed
 *           into the dialogResultHandler associated with this presenter.
 *
 * @author rb250096
 * @version $Revision: 2026 $
 */
public class DialogPresenter<T extends DialogView, R> extends Presenter<T> {

    //--------------------------------------------------------------------------
    //
    //  Fields
    //
    //--------------------------------------------------------------------------

    /**
     * Delegate invoked when the dialog is closed.  Optionally, a result
     * value from the dialog being managed by the presenter will be passed
     * as a single argument to the delegate.
     */
    protected ParameterizedDelegate<Void, R> dialogResultHandler;

    //--------------------------------------------------------------------------
    //
    //  Constructors
    //
    //--------------------------------------------------------------------------

    /**
     * Constructor.
     */
    public DialogPresenter() {
        super();
        this.setAutoSetup(false);
    }

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    /**
     * Gets delegate invoked when the dialog is closed.  Optionally, a result
     * value from the dialog being managed by the presenter will be passed
     * as a single argument to the delegate.
     *
     * @return The delegate.
     */
    public ParameterizedDelegate<Void, R> getDialogResultHandler() {
        return this.dialogResultHandler;
    }

    /**
     * Sets delegate invoked when the dialog is closed.  Optionally, a result
     * value from the dialog being managed by the presenter will be passed
     * as a single argument to the delegate.
     *
     * @param dialogResultHandler The result handler.
     */
    public void setDialogResultHandler(ParameterizedDelegate<Void, R> dialogResultHandler) {
        this.dialogResultHandler = dialogResultHandler;
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     * Closes the view being managed this presenter and invokes the
     * {@link #tearDown()} method.
     */
    public void close() {
        this.view.close();
        this.tearDown();
    }

    /**
     * Closes the view being managed by this presenter and invokes the
     * {@link #tearDown()} method.  Additionally, this method will invoke
     * the {@link #dialogResultHandler} using the <code>dialogResult</code>
     * object as its argument.
     *
     * @param dialogResult The result to pass into the dialogResultHandler.
     */
    public void closeWithResult(R dialogResult) {
        this.close();

        if(this.dialogResultHandler != null) {
            this.dialogResultHandler.invoke(dialogResult);
        }
    }
}
